//
//  TRoadLoadingView.m
//  TRoad2
//
//  Created by ZhangYanhua on 15/9/14.
//  Copyright (c) 2015年 Mach. All rights reserved.
//

#import "TRoadLoadingView.h"
#import "QWKitConfig.h"

@interface TRoadLoadingView() {
    CGFloat _offsetY;
}

@end

@implementation TRoadLoadingView

- (id)initWithCoder:(NSCoder *)aDecoder
{
    self = [super initWithCoder:aDecoder];
    if (self) {
        [self commonInit];
    }
    return self;
}
- (id)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self commonInit];
    }
    return self;
}

- (void)commonInit
{
    _loadingView = [[UIImageView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 150.0f, 150.0f)];
    _loadingView.contentMode = UIViewContentModeCenter;
    [self addSubview:_loadingView];
    
    _activityIndicatorView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
    _activityIndicatorView.hidesWhenStopped = YES;
    _activityIndicatorView.color = [UIColor grayColor];
    [self addSubview:_activityIndicatorView];
    
    _activityLabel = [[UILabel alloc] initWithFrame:CGRectZero];
    _activityLabel.font = CUSTOM_FONT(17.0f);
    _activityLabel.textAlignment = NSTextAlignmentCenter;
    _activityLabel.textColor = NO_CONTENT_INFO_COLOR_TROAD;
    _activityLabel.backgroundColor = [UIColor clearColor];
    _activityLabel.numberOfLines = 0;
    [self addSubview:_activityLabel];

    _infoLabel = [[UILabel alloc] initWithFrame:CGRectZero];
    _infoLabel.font = CUSTOM_FONT(17.0f);
    _infoLabel.textAlignment = NSTextAlignmentCenter;
    _infoLabel.textColor = NO_CONTENT_INFO_COLOR_TROAD;
    _infoLabel.backgroundColor = [UIColor clearColor];
    _infoLabel.numberOfLines = 0;
    [self addSubview:_infoLabel];
    
    _actionButton = [QWRoundCornerButton buttonWithType:UIButtonTypeCustom];
    [_actionButton setTitle:@"刷新" forState:UIControlStateNormal];
    [_actionButton setTitleColor:BLUE_HIGHLIGHT_COLOR forState:UIControlStateNormal];
    [_actionButton.titleLabel setFont:[UIFont systemFontOfSize:12.0f]];
    [_actionButton setNormalBorderColor:BLUE_HIGHLIGHT_COLOR];
    [_actionButton setNormalFillColor:[UIColor clearColor]];
    [self addSubview:_actionButton];
    
    [_actionButton addTarget:self
                      action:@selector(actionButtonPressed:)
            forControlEvents:UIControlEventTouchUpInside];
}

- (void)adjustOffsetY:(CGFloat)offsetY;
{
    _offsetY = offsetY;
    
    [self setNeedsLayout];
}

- (void)layoutSubviews {
    [super layoutSubviews];
    
    [self.infoLabel sizeToFit];
    [self.actionButton sizeToFit];
    [self.activityLabel sizeToFit];
    
    CGSize activitySz = [self.activityLabel sizeThatFits:CGSizeMake(kQWKitScreenWidth - 60.0f, CGFLOAT_MAX)];
    
    CGFloat imageHeight = self.loadingView.image ? self.loadingView.image.size.height : 0.0f;
    
    CGFloat totalHeight = self.infoLabel.bounds.size.height + imageHeight + 40.0f;
    
    CGFloat startY = ((self.bounds.size.height - totalHeight) / 2.0f);
    CGFloat deltaY = (self.loadingView.frame.size.height - imageHeight);
    deltaY = MAX(0.0f, deltaY);
    startY = roundf(startY - deltaY / 2.0f) - 40.0f;
    startY = MAX(10.0f, startY);
    
    startY += _offsetY;
    
    self.loadingView.frame = CGRectMake(roundf((self.bounds.size.width - self.loadingView.frame.size.width) / 2.0f),
                                        startY,
                                        self.loadingView.frame.size.width,
                                        self.loadingView.frame.size.height);
    
    self.infoLabel.frame = CGRectMake(30.0f,
                                      roundf(self.loadingView.center.y + imageHeight / 2.0f + 20.0f),
                                      kQWKitScreenWidth - 60,
                                      self.infoLabel.frame.size.height);
    
    self.activityIndicatorView.center = CGPointMake(self.bounds.size.width / 2.0f, CGRectGetMinY(self.infoLabel.frame) - self.activityIndicatorView.frame.size.height / 2.0f - 10.0f);
    
    self.activityLabel.frame = CGRectMake(30.0f, CGRectGetMaxY(self.activityIndicatorView.frame) + 20.0f,
                                          kQWKitScreenWidth - 60.0f, ceilf(activitySz.height));
    
    CGFloat btnWidht = MAX(80.0f, self.actionButton.bounds.size.width + 10.0f);
    self.actionButton.frame = CGRectMake((self.bounds.size.width - 80.0f) / 2.0f, CGRectGetMaxY(self.infoLabel.frame) + 20.0f, btnWidht, 25.0f);
}

#pragma mark -
#pragma mark public methods

+ (TRoadLoadingView *)showLoadingViewAddedToView:(UIView *)view
                                  actionDelegate:(id<TroadLoadingViewDelegate>)actionDelegate
{
    TRoadLoadingView *loadingView = [[TRoadLoadingView alloc] initWithFrame:view.bounds];
    loadingView.delegate = actionDelegate;

    loadingView.hidden = NO;
    loadingView.loadingView.image = nil;
    [loadingView.activityIndicatorView startAnimating];
    loadingView.infoLabel.text = nil;
    loadingView.actionButton.hidden = YES;
    [loadingView.loadingView startAnimating];

    loadingView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;

    [view addSubview:loadingView];
    [loadingView setNeedsLayout];

    return loadingView;
}

+ (void)hideAllLoadingViewsForView:(UIView *)view
{
    NSMutableArray *loadingViews = [NSMutableArray array];
    NSArray *subviews = view.subviews;
    for (UIView *aView in subviews) {
        if ([aView isKindOfClass:self]) {
            [loadingViews addObject:aView];
        }
    }

    for (TRoadLoadingView *loadingView in loadingViews) {
        [loadingView hideLoadingView];
    }
}

- (void)showLoadingFailedViewWithErrorImage:(UIImage *)errorImg
                                   errorMsg:(NSString *)errorMsg
                           showActionButton:(BOOL)showActionBtn;
{
    NSAssert((!showActionBtn || [self.delegate respondsToSelector:@selector(loadingView:actionButtonPressed:)]), @"显示actionButton的时候一定要实现TRoadLoadingViewProtocol");

    self.hidden = NO;
    [self.activityIndicatorView stopAnimating];
    [self.loadingView stopAnimating];
    self.loadingView.animationImages = nil;
    self.loadingView.image = errorImg;
    self.infoLabel.text = errorMsg;
    self.actionButton.hidden = !showActionBtn;
    [self.activityLabel setText:nil];
    [self setNeedsLayout];
}

- (void)showLoadingView
{
    self.hidden = NO;
    self.loadingView.image = nil;
    [self.activityIndicatorView startAnimating];
    self.infoLabel.text = nil;
    self.actionButton.hidden = YES;
    [self.loadingView startAnimating];
    [self setNeedsLayout];
}

- (void)hideLoadingView
{
    [self.activityIndicatorView stopAnimating];
    self.hidden = YES;
    [self removeFromSuperview];
}

- (void)actionButtonPressed:(id)sender
{
    if ([self.delegate respondsToSelector:@selector(loadingView:actionButtonPressed:)]) {
        [self.delegate loadingView:self actionButtonPressed:sender];
    }
}

@end
